package com.apex.shared.mybatis.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Apex MybatisMetaObjectHandler
 *
 * @author Bangda Xiao
 */
@Slf4j
@Component
public class AutoFillHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("执行insert自动填充---------------");
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDate = sdf.format(date);
       this.setFieldValByName("createdTime", date, metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("执行update自动填充---------------");
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDate = sdf.format(date);
        this.setFieldValByName("updateTime", date, metaObject);
    }
}
